﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="ReturnJsonBase.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：ReturnJsonBase.cs
// 创建时间：2015-03-12 16:58
// 修改时间：2015-03-10 1:16
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using System; 
using Newtonsoft.Json;

namespace Jiekou.Com
{
    #region

    

    #endregion

    /// <summary>
    ///     返回json
    /// </summary>
    [JsonObject(MemberSerialization.OptIn)]
    public class RetJson {
        ///// <summary>
        /////     备用字段
        ///// </summary>
        // [JsonProperty("bak")]
        // public string Bak { get; set; }
        #region Fields

        /// <summary>
        /// The json.
        /// </summary>
        private readonly SubJson json;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ReturnJsonBase"/> class.
        /// </summary>
        /// <param name="json">
        /// SubJson
        /// </param>
        public RetJson(SubJson json)
        { 
            this.json = json;
            this.Fn = json.Fn;
            this.Suid = json.Suid.ToString();
            this.Jsondata = json.Jsondata;
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     数据集
        /// </summary>
        [JsonProperty("data")]
        public object Data { get; set; }

        /// <summary>
        ///     功能码
        /// </summary>
        [JsonProperty("fn")]
        public string Fn { get; set; }

        /// <summary>
        ///     Gets the json string.
        /// </summary>
        public string JsonString { get; private set; }

        /// <summary>
        ///     subdata
        /// </summary>
        public string Jsondata { get; protected set; }

        /// <summary>
        ///     消息
        /// </summary>
        [JsonProperty("msg")]
        public string Msg { get; set; }

        /// <summary>
        ///     状态
        /// </summary>
        [JsonProperty("status")]
        public bool Status { get; set; }

        /// <summary>
        ///     系统用户id
        /// </summary>
        [JsonProperty("suid")]
        
        public string Suid { get; set; }

        #endregion

        #region Properties

        /// <summary>
        ///     时间
        /// </summary>
        [JsonProperty("time")]
        protected string Time
        {
            get
            {
                return DateTime.Now.ToString("yyyyMMddHHmmss");
            }
        }

        #endregion

        #region Public Methods and Operators
 

         
        public string GetJson(IJsonData data)
        {
            this.Data = data.SetData(this.Jsondata);
            this.Msg = data.Msg;
            if (string.IsNullOrWhiteSpace(this.Msg))
            {
                this.Status = true;
            }
            else
            {
                this.Status = false;
            }

            var jsons = new JsonSerializerSettings();
            jsons.NullValueHandling = NullValueHandling.Ignore;
            this.JsonString = JsonConvert.SerializeObject(this, jsons);
            return this.JsonString;
        }

        #endregion
    }
}